﻿using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Xml;
using Microsoft.SharePoint.Administration;
using System.Web.Caching;
using System.Web.UI;
using System.Xml.Xsl;
using System.Net;
using System.Data.SqlClient;
using VPLink2013.Framework;
using VPLink2013.Framework.Constants;
using VPLink2013.Framework.Extensions;

namespace VPLink2013.Webcontrols
{
    public class NosSites: WebcontrolVPLink
    {
        protected override void Render(HtmlTextWriter writer)
        {
            Template = "webcontrol NosSites";
            try
            {
                if (!string.IsNullOrEmpty(siteUrl))
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.RootWeb)
                        {
                            SPList lst = web.Lists.TryGetList(ListTitles.NosSitesTitle);
                            if (lst != null)
                            {
                                ProcessNosSites(lst);
                            }
                        }
                    }
                }
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("webcontrol NosSites.Erreur.Render : " + exc.Message + "/" + exc.StackTrace);
            }
            base.Render(writer);
        }

        private void ProcessNosSites(SPList lst)
        {
            SPQuery spQuery = new SPQuery();
            StringBuilder sb = new StringBuilder();
            sb.Append("<Where>");
            sb.Append("<Eq><FieldRef Name='Visible'/><Value Type='Text'>Oui</Value></Eq>");
            sb.Append("</Where>");
            sb.Append("<OrderBy><FieldRef Name='Ordre' Ascending='TRUE'/><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>");
            string Query = sb.ToString();
            spQuery.Query = Query;
            ContextTrace.AddTrace("spQuery.Query : " + spQuery.Query);

            SPListItemCollection items = lst.GetItems(spQuery);
            //int intWidth = 100 / items.Count; 

            sbHtml.AppendLine("<ul>");
            foreach (SPListItem item in items)
            {
                try
                {
                    CreateSiteLink(item);
                }
                catch (Exception ex)
                {
                    ContextTrace.AddTrace("Exception lors de la creation d'un item nossites " + ex.Message);
                    ContextTrace.AddTrace(ex.StackTrace);
                }
            }
            sbHtml.AppendLine("</ul>");
        }

        private void CreateSiteLink(SPListItem item)
        {
            ContextTrace.AddTrace("-------------------------------");
            string strLinkURL = item.GetItemListFieldValueAsUrlValue(Fields.UrlNewsField);
            string strTexteURL = item.GetItemListFieldValueAsUrlDescription(Fields.UrlNewsField);
            string strTextePiwik = strTexteURL.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
            ContextTrace.AddTrace("strTexteURL : " + strTexteURL);
            ContextTrace.AddTrace("strLinkURL : " + strLinkURL);

            string strLinkImage = item.GetItemListFieldValueAsUrlValue(Fields.ImageField);
            string strTexteImage = item.GetItemListFieldValueAsUrlDescription(Fields.ImageField);

            ContextTrace.AddTrace("strLinkImage : " + strLinkImage);
            ContextTrace.AddTrace("strTexteImage : " + strTexteImage);

            strLinkImage = "<img src=\"" + strLinkImage + "\" alt=\"" + strTexteImage + "\">";

            string strURL = "<a href=\"" + strLinkURL + "\" onclick=\"javascript:_paq.push(['trackPageView', 'NOS SITES/" + strTextePiwik + "']);\" target=\"_blank\">" + strLinkImage + "</a>";
            ContextTrace.AddTrace("strURL : " + strURL);
            sbHtml.AppendLine("<li>");
            sbHtml.AppendLine(strURL);
            sbHtml.AppendLine("</li>");
        }
    }
}
